package cn.iocoder.yudao.module.bpm.service.taggroupinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.taggroupinfo.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.taggroupinfo.BpmTagGroupInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.taggroupinfo.BpmTagGroupInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 标签组信息 Service 实现类
 *
 * @author 韩佳乐
 */
@Service
@Validated
public class BpmTagGroupInfoServiceImpl implements BpmTagGroupInfoService {

    @Resource
    private BpmTagGroupInfoMapper tagGroupInfoMapper;

    @Override
    public Long createTagGroupInfo(BpmTagGroupInfoSaveReqVO createReqVO) {
        // 插入
        BpmTagGroupInfoDO tagGroupInfo = BeanUtils.toBean(createReqVO, BpmTagGroupInfoDO.class);
        tagGroupInfoMapper.insert(tagGroupInfo);
        // 返回
        return tagGroupInfo.getId();
    }

    @Override
    public void updateTagGroupInfo(BpmTagGroupInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateTagGroupInfoExists(updateReqVO.getId());
        // 更新
        BpmTagGroupInfoDO updateObj = BeanUtils.toBean(updateReqVO, BpmTagGroupInfoDO.class);
        tagGroupInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteTagGroupInfo(Long id) {
        // 校验存在
        validateTagGroupInfoExists(id);
        // 删除
        tagGroupInfoMapper.deleteById(id);
    }

    private void validateTagGroupInfoExists(Long id) {
        if (tagGroupInfoMapper.selectById(id) == null) {
            throw exception(TAG_GROUP_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BpmTagGroupInfoDO getTagGroupInfo(Long id) {
        return tagGroupInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BpmTagGroupInfoDO> getTagGroupInfoPage(BpmTagGroupInfoPageReqVO pageReqVO) {
        return tagGroupInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmAllTagGroupInfoVO> getAllTagGroupInfo() {

        return tagGroupInfoMapper.selectAllTagChildren();
    }

}